namespace QMVnSoft.Controls
{
    using QMVnSoft;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class TextControl : TextBox
    {
        private Container components;
        private bool ib_CheckTextSearched;
        private bool ib_FollowedSystemFont = true;
        private Color ic_DisabledForeColor = Color.Blue;
        private AppInstance.TextControlLength ie_UserTextLength;
        private AppInstance.TextControlTrimMode ie_UserTrimMode = AppInstance.TextControlTrimMode.Right;
        private object oOldValue;

        public TextControl()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnCreateControl()
        {
            if (this.FollowedSystemFont)
            {
                this.Font = AppInstance.SystemFont;
            }
            if (!base.Enabled)
            {
                base.SetStyle(ControlStyles.UserPaint, true);
            }
            base.OnCreateControl();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            if (!this.CheckTextSearched)
            {
                this.OldValue = this.Value;
            }
            else
            {
                this.CheckTextSearched = false;
            }
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            switch (this.UserTrimMode)
            {
                case AppInstance.TextControlTrimMode.Left:
                    this.Text = this.Text.TrimStart(new char[0]);
                    break;

                case AppInstance.TextControlTrimMode.Right:
                    this.Text = this.Text.TrimEnd(new char[0]);
                    break;

                case AppInstance.TextControlTrimMode.All:
                    this.Text = this.Text.Trim();
                    break;
            }
            base.OnLostFocus(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (!base.Enabled)
            {
                SolidBrush brush = new SolidBrush(this.DisabledForeColor);
                e.Graphics.DrawString(this.Text, this.Font, brush, (float) 0f, (float) 0f);
            }
        }

        private void SetTextLength()
        {
            switch (this.UserTextLength)
            {
                case AppInstance.TextControlLength.None:
                    break;

                case AppInstance.TextControlLength.InvoiceCode:
                    this.MaxLength = 4;
                    return;

                case AppInstance.TextControlLength.InvoiceNumber:
                    this.MaxLength = 20;
                    return;

                case AppInstance.TextControlLength.ItemCode:
                    this.MaxLength = 0x20;
                    return;

                case AppInstance.TextControlLength.ItemUnit:
                    this.MaxLength = 20;
                    break;

                case AppInstance.TextControlLength.ItemDescription:
                    this.MaxLength = 128;
                    return;

                case AppInstance.TextControlLength.InvoiceDescription:
                    this.MaxLength = 0xfe;
                    return;

                default:
                    return;
            }
        }

        public bool CheckTextSearched
        {
            get
            {
                return this.ib_CheckTextSearched;
            }
            set
            {
                if (this.ib_CheckTextSearched != value)
                {
                    this.ib_CheckTextSearched = value;
                }
            }
        }

        [Description("To get or set disabled color."), RefreshProperties(RefreshProperties.All), Category("AppTextControl properties")]
        public Color DisabledForeColor
        {
            get
            {
                return this.ic_DisabledForeColor;
            }
            set
            {
                this.ic_DisabledForeColor = value;
            }
        }

        [DefaultValue(true)]
        public bool FollowedSystemFont
        {
            get
            {
                return this.ib_FollowedSystemFont;
            }
            set
            {
                this.ib_FollowedSystemFont = value;
            }
        }

        public object OldValue
        {
            get
            {
                return this.oOldValue;
            }
            set
            {
                if (this.oOldValue != value)
                {
                    this.oOldValue = value;
                }
            }
        }

        [RefreshProperties(RefreshProperties.All), Category("AppTextControl properties"), Description("Indicating the length of text."), DefaultValue(0)]
        public AppInstance.TextControlLength UserTextLength
        {
            get
            {
                return this.ie_UserTextLength;
            }
            set
            {
                this.ie_UserTextLength = value;
                this.SetTextLength();
            }
        }

        [DefaultValue(2), RefreshProperties(RefreshProperties.All), Category("AppTextControl properties"), Description("Indicating the trim mode of text.")]
        public AppInstance.TextControlTrimMode UserTrimMode
        {
            get
            {
                return this.ie_UserTrimMode;
            }
            set
            {
                this.ie_UserTrimMode = value;
            }
        }

        [Browsable(false), Description("The current value displayed in the textbox."), Bindable(true), RefreshProperties(RefreshProperties.All), Category("AppTextControl properties")]
        public object Value
        {
            get
            {
                if (this.Text.Equals(string.Empty))
                {
                    return DBNull.Value;
                }
                return this.Text;
            }
            set
            {
                this.Text = value.ToString();
            }
        }

        [Category("AppTextControl properties"), Browsable(false), RefreshProperties(RefreshProperties.All), Description("Indicating value has been changed.")]
        public bool ValueChanged
        {
            get
            {
                return !this.OldValue.Equals(this.Value);
            }
        }
    }
}

